package weaver.interfaces.zlldjcws.cgsh.service.impl;

import weaver.interfaces.zlldjcws.util.Message;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.interfaces.zlldjcws.cgsh.bean.CgshBean;
import weaver.interfaces.zlldjcws.cgsh.service.LdCgshService;

import javax.ejb.Stateless;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ZhangLei
 * @date 2021/8/3
 * @tableName uf_PurchaseReceipt
 */

@Stateless(mappedName = "LdCgshServiceImpl")
@WebService(serviceName = "LdCgshServiceImpl")
public class LdCgshServiceImpl extends BaseBean implements LdCgshService {
    @Override
    public Message cgshData(CgshBean cgshBean) {
        Message message = new Message();
        String[] params = {"BUDAT","MBLNR","MJAHR","ZEILE","MATNR","MAKTX","WERKS","ERFMG","CHARG","ERFME","LGORT","EBELN","EBELP","LFBNR","LFPOS","BWART","SMBLN","SMBLP"};
        writeLog("表对象信息：");
        writeLog(cgshBean);
        Map<String, Object> datas = new HashMap<>();
        datas.put("budat",cgshBean.getBudat());
        datas.put("mblnr",cgshBean.getMblnr());
        datas.put("mjahr",cgshBean.getMjahr());
        datas.put("zeile",cgshBean.getZeile());
        datas.put("matnr",cgshBean.getMatnr());
        datas.put("maktx",cgshBean.getMaktx());
        datas.put("werks",cgshBean.getWerks());
        datas.put("erfmg",cgshBean.getErfmg());
        datas.put("charg",cgshBean.getCharg());
        datas.put("lgort",cgshBean.getLgort());
        datas.put("ebeln",cgshBean.getEbeln());
        datas.put("ebelp",cgshBean.getEbelp());
        datas.put("lfbnr",cgshBean.getLfbnr());
        datas.put("lfpos",cgshBean.getLfpos());
        datas.put("bwart",cgshBean.getBwart());
        datas.put("smbln",cgshBean.getSmbln());
        datas.put("smblp",cgshBean.getSmblp());
//        如果物料凭证编号MBLNR无
        String sapmblnr = cgshBean.getMblnr();
        if ("".equals(sapmblnr) || sapmblnr == null){
            message.setId("-200");
            message.setFlag("E");
            message.setMessage("物料凭证编号MBLNR为空");
        }else {
            writeLog("SAP传递的mblnr值为："+sapmblnr);
            String sureMblnrExSql = "select mblnr from uf_PurchaseReceipt where mblnr = "+sapmblnr;
            RecordSet dataMblnrRs = new RecordSet();
            dataMblnrRs.execute(sureMblnrExSql);
            String dataMblnr = "";
            if (dataMblnrRs.next()){
                dataMblnr = dataMblnrRs.getString("mblnr");
            }
//            如果dataMblnr有值便说明数据库存在物料凭证编号为sapmblnr的记录
            writeLog("数据库中存储的Mblnr值："+dataMblnr);
            //数据库中没有物料凭证编号为sapmblnr的记录：
            if ("".equals(dataMblnr) || dataMblnr==null){
                message.setId("-200");
                message.setFlag("E");
                message.setMessage("物料凭证编号MBLNR不存在，请检查传递的参数");
            }else {
                //存在则更新数据库
                String updateSql = updateSql(params, datas, "uf_PurchaseReceipt");
                RecordSet updateRs = new RecordSet();
                boolean updateResult = updateRs.executeUpdate(updateSql);
                if (updateResult){
                    //根据需求，更新采购订单表单(formtable_main_126_dt1)的已收货数量(YSHSL)，
                    // 当全部收货完成，更新采购订单收货状态(CGDDZT)为已完成。
                    String sfywcSql = "select MENGE from formtable_main_126_dt1 where MATNR=?";
                    RecordSet sfywcRs = new RecordSet();
                    sfywcRs.executeQuery(sfywcSql,cgshBean.getMatnr());
                    String dataMenge="";
                    if (sfywcRs.next()){
                        dataMenge = sfywcRs.getString("MENGE");
                    }
                    writeLog("采购订单表中的采购数量为："+dataMenge);
                    writeLog("SAP传递的采购收货数量为："+cgshBean.getErfmg());

                    if (dataMenge.equals(cgshBean.getErfmg())){
                        //sap传递的已收货数量 = 采购订单表中的采购数量
                        String updateTwo = "update from formtable_main_126_dt1 set YSHSL=?,CGDDZT='已完成'";
                        RecordSet updateTwoRs = new RecordSet();
                        boolean update = updateTwoRs.executeUpdate(updateTwo, cgshBean.getErfmg());
                        if (update){
                            //更新成功
                            message.setId("200");
                            message.setFlag("S");
                            message.setMessage("Success!");
                        }else {
                            //更新失败
                            message.setId("-200");
                            message.setFlag("E");
                            message.setMessage("采购订单表更新失败，请检查传递的参数");
                        }
                    }else {
                        //sap传递的已收货数量 != 采购订单表中的采购数量
                        String updateOneSql = "update from formtable_main_126_dt1 set YSHSL=?";
                        RecordSet updateOneRs = new RecordSet();
                        boolean update = updateOneRs.executeUpdate(updateOneSql);
                        if (update){
                            //更新成功
                            message.setId("200");
                            message.setFlag("S");
                            message.setMessage("Success!");
                        }else {
                            //更新失败
                            message.setId("-200");
                            message.setFlag("E");
                            message.setMessage("采购订单表更新失败，请检查传递的参数");
                        }
                    }
                }else {
                    message.setId("-200");
                    message.setFlag("E");
                    message.setMessage("数据库更新失败，请检查传递的参数");
                }
            }
        }

        return message;

    }

    /**
     * update语句的组装(需要自行添加where条件语句)
     */
    private static String updateSql(String[] params, Map<String, Object> datas, String tableName) {
        StringBuilder updateSql = new StringBuilder();
        updateSql.append("update ").append(tableName).append(" set ");
        for (int i = 0; i < params.length; i++) {
            if (datas.get(params[i].toLowerCase()) == null){
                updateSql.append(params[i].toLowerCase()).append("=").append("null");
            }else {
                updateSql.append(params[i].toLowerCase()).append("='").append(datas.get(params[i].toLowerCase())).append("'");
            }

            if (i != params.length - 1) {
                updateSql.append(",");
            }
        }
        return updateSql.toString();
    }
}
